/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package LvodUpdater;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import lvoddownloader.QvodPlayerUI;

/**
 *
 * @author wcss
 */
public class CheckUpdate implements Runnable
{
   public static HttpGet downloadTools = new HttpGet();
   public static String updateCache = "";
   public static String updateConfigCache = "";
   public static String updateFilesCache = "";
   public static int maxUpdateVersion = 0;
   public static int needDownloadFileCount = 0;
   public static ArrayList<String> updateRemoteList = new ArrayList<String>();
   public static ArrayList<String> updateLocalList = new ArrayList<String>();
   
   public static int checkUpdate()
   {
        //载入配置文件
        UpdateConfig.configPath = "/etc/lvoddownloader_update.cfg";
        File f = new File("/etc/lvoddownloader_update.cfg");
        if (f.exists()) {
            try {
                UpdateConfig.config.loadConfig();
                CheckUpdate.updateCache = JAppToolKit.JRunHelper.getUserHomeDirPath() + "/.gameautoupdatecache";
                CheckUpdate.updateConfigCache = JAppToolKit.JRunHelper.getUserHomeDirPath() + "/.gameautoupdatecache/lists";
                CheckUpdate.updateFilesCache = JAppToolKit.JRunHelper.getUserHomeDirPath() + "/.gameautoupdatecache/files";
                File dir1 = new File(CheckUpdate.updateCache);
                if (dir1.exists()) {
                    dir1.delete();
                }
                dir1.mkdirs();
                File dir2 = new File(CheckUpdate.updateConfigCache);
                dir2.mkdirs();
                File dir3 = new File(CheckUpdate.updateFilesCache);
                dir3.mkdirs();

                downloadTools.addItem(UpdateConfig.config.listUrl, CheckUpdate.updateCache + "/update.list");
                downloadTools.addItem(UpdateConfig.config.updateInfoUrl, CheckUpdate.updateCache + "/updatelog.txt");
                downloadTools.downLoadByList();
                downloadTools.resetList();
                String[] teams = JAppToolKit.JDataHelper.readAllLines(CheckUpdate.updateCache + "/update.list");
                for (String url : teams) {
                    CheckUpdate.updateRemoteList.add(url);
                }

                //下载更新配置文件
                int uindex = 0;
                int needdownloadcount = 0;
                downloadTools.resetList();
                for (Iterator<String> it = CheckUpdate.updateRemoteList.iterator(); it.hasNext();) {
                    String urls = it.next();
                    uindex++;
                    String locals = CheckUpdate.updateConfigCache + "/update-" + uindex + ".cfg";
                    downloadTools.addItem(urls, locals);
                    CheckUpdate.updateLocalList.add(locals);
                }
                downloadTools.downLoadByList();
                downloadTools.resetList();
                for (String loca : updateLocalList) {
                    UpdateList ull = new UpdateList();
                    ull.loadList(loca);
                    if (ull.versionIndex > UpdateConfig.config.currentVersion) {
                        if (ull.versionIndex > maxUpdateVersion) {
                            maxUpdateVersion = ull.versionIndex;
                        }
                        needdownloadcount++;
                        CheckUpdate.needDownloadFileCount += ull.files.size();
                    }
                }
                
                return needdownloadcount;
            }catch(Exception ex)
            {
                ex.printStackTrace();
                
                return 0;
            }
        }else
        {
            return 0;
        }
   }
   
   public static void startUpdate()
   {
       Thread tt = new Thread(new CheckUpdate());
       tt.setDaemon(true);
       tt.start();
   }
   
   public static void main(String[] args)
   {
       CheckUpdate cu = new CheckUpdate();
       System.out.println("Update:" + cu.checkUpdate());
   }

    @Override
    public void run() 
    {
        try {
            Thread.sleep(7000);
        } catch (InterruptedException ex) {
            Logger.getLogger(CheckUpdate.class.getName()).log(Level.SEVERE, null, ex);
        }
        int updatecount = 0;
        try
        {
            updatecount = CheckUpdate.checkUpdate();
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        
        if (updatecount > 0)
        {
            int result = javax.swing.JOptionPane.showConfirmDialog(null, "有更新需要下载，现在更新吗？", "更新提示",javax.swing.JOptionPane.YES_NO_OPTION);
            if (result == 0)
            {
                try {
                    //需要下载
                    JAppToolKit.JRunHelper.runSysCmd("/opt/lvodDownloader/startupdate.sh", false);
                    System.exit(0);
                } catch (Exception ex) {
                    Logger.getLogger(QvodPlayerUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

}
